/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 23/05/2005
 * Time: 13:56
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;

namespace Orz.LogManager
{
/// <summary>
/// Description of HelpForm.
/// </summary>
public class HelpForm : System.Windows.Forms.Form
{
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Panel panel1;
		
		
		
		private const int MAX_WIDTH = 300;
		private HeightLabel label_i;
		
		
	public HelpForm(Form form_p)
	{
		InitializeComponent();
		CreateLabel();
		
		form_p.HelpButton = true;
		
		AddHandlerToChildren (form_p);		
	
	}
	
	
	
	private void CreateLabel()
	{
		panel1.SuspendLayout();
		// 
		// label1
		// 
		label_i = new HeightLabel();
		label_i.Name = "label_i";
		label_i.Location = new Point(8,8);
		label_i.Size = new Size(100,100);
		label_i.BackColor = SystemColors.Info;
		label_i.Click += new System.EventHandler(this.Label1Click);
		panel1.Controls.Add(label_i);
				
		panel1.ResumeLayout(false);
	}
	
	
	
	
	public void AddHandlerToChildren (Control c)
	{		
		IEnumerator i = c.Controls.GetEnumerator();
		while (i.MoveNext())
		{
			Control child = (Control) i.Current;
			
			child.HelpRequested += new HelpEventHandler(this.ShowHelp);
			
			AddHandlerToChildren (child);
		}
	}
	
	
	
	
	private void ShowHelp (object sender, HelpEventArgs e)
	{		
		Control c = (Control) sender;
		
		string sText = (string) c.Tag;
		
		if (null == sText  ||  0 == sText.Length)
		{
			sText = "Sorry - no help text defined for this item.";
		}
		
		this.label_i.Text = sText;
		UpdateLabelSize();
		UpdateLabelPosition(c);
		this.Show();
		e.Handled = true;
	}
	
	
	
	private void UpdateLabelSize()
	{
		if (label_i.PreferredWidth < MAX_WIDTH)
		{
			label_i.Size = new Size(label_i.PreferredWidth, label_i.PreferredHeight);
		}
		else
		{
			int iNumLines = ((int) (label_i.PreferredWidth * 1.2) / MAX_WIDTH) + 1;
			int iHeight = iNumLines * label_i.LineHeight;
			
			label_i.Size = new Size(MAX_WIDTH, iHeight);
		}
		
		panel1.Width = label_i.Width + 16;
		panel1.Height = label_i.Height + 16;
		panel2.Width = panel1.Width;
		panel2.Height = panel1.Height;
		
		
		this.Width = panel2.Right + 16;
		this.Height = panel2.Bottom + 16;
	}
	
	
	private void UpdateLabelPosition (Control c)
	{
		System.Drawing.Point p = c.PointToScreen(new Point(0, c.Height));
		
		p.Offset(8, 8);
		
		// Make sure the form won't be off the screen
		Rectangle rect = Screen.GetWorkingArea(p);
		rect.Width -= this.Width;
		rect.Height -= this.Height;
		
		
		
		if (p.X < rect.Left) 	p.X = rect.Left;
		if (p.Y < rect.Top) 	p.Y = rect.Top;
		if (p.X > rect.Right) 	p.X = rect.Right;
		if (p.Y > rect.Bottom) 	p.Y = rect.Bottom;
		
		this.Location = p;		
	}
	
	
	
	
	
	
	void HelpFormDeactivate(object sender, System.EventArgs e)
	{
		this.Hide();
	}
	
	void Label1Click(object sender, System.EventArgs e)
	{
		this.Hide();
	}
	
	
	
	
	
	#region Windows Forms Designer generated code
	/// <summary>
	/// This method is required for Windows Forms designer support.
	/// Do not change the method contents inside the source code editor. The Forms designer might
	/// not be able to load this method if it was changed manually.
	/// </summary>
	private void InitializeComponent() {
		this.panel1 = new System.Windows.Forms.Panel();
		this.panel2 = new System.Windows.Forms.Panel();
		this.SuspendLayout();
		// 
		// panel1
		// 
		this.panel1.BackColor = System.Drawing.SystemColors.Info;
		this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
		this.panel1.Location = new System.Drawing.Point(8, 8);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(392, 72);
		this.panel1.TabIndex = 1;
		this.panel1.Click += new System.EventHandler(this.Label1Click);
		// 
		// panel2
		// 
		this.panel2.BackColor = System.Drawing.SystemColors.ControlDarkDark;
		this.panel2.Location = new System.Drawing.Point(12, 12);
		this.panel2.Name = "panel2";
		this.panel2.Size = new System.Drawing.Size(392, 72);
		this.panel2.TabIndex = 1;
		this.panel2.Click += new System.EventHandler(this.Label1Click);
		// 
		// HelpForm
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
		this.ClientSize = new System.Drawing.Size(520, 102);
		this.Controls.Add(this.panel1);
		this.Controls.Add(this.panel2);
		this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
		this.KeyPreview = true;
		this.Name = "HelpForm";
		this.Opacity = 0.95D;
		this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
		this.Text = "HelpForm";
		this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
		this.Deactivate += new System.EventHandler(this.HelpFormDeactivate);
		this.Click += new System.EventHandler(this.Label1Click);
		this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HelpFormKeyDown);
		this.ResumeLayout(false);
		}
	#endregion
	
	void HelpFormKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
	{
		this.Hide();
	}
	
}






class HeightLabel : Label
{
	public int LineHeight
	{
		get { return this.FontHeight; }
	}
}
}
